package net.niufei;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;

import net.niufei.util.Charset;


public interface DataBuffer {

    ByteBuffer asByteBuffer();

    /**
     * Return this DataBuffer is permanent. Permanent DataBuffer can't be released.
     * 
     * @return is permanent
     */
    boolean isPermanent();

    /**
     * Set current DataBuffer is permanent. Permanent DataBuffer can't be released.
     * 
     * @param b
     *            permanent
     */
    void setPermanent(boolean b);

    /**
     * Release this buffer's content if not permanent.
     */
    void release();

    /**
     * Current buffer's content have been released.
     * 
     * @return released
     */
    boolean isReleased();

    /**
     * Writes a sequence of bytes to the channel.
     * 
     * @param channel
     *            channel
     * @return the number of bytes written, possibly zero
     * @throws IOException
     *             any io exception
     */
    int write(WritableByteChannel channel) throws IOException;

    /**
     * Reads a sequence of bytes from the channel.
     * 
     * @param channel
     *            channel
     * @return the number of bytes read, possibly zero, or -1 if the channel has
     *         reached end-of-stream
     * @throws IOException
     *             any io exception
     */
    int read(ReadableByteChannel channel) throws IOException;

    /**
     * This method is a shorthand for: position(position() + size).
     * 
     * @param size
     *            skip size
     * @return the byte buffer
     */
    DataBuffer skip(int size);

    /**
     * Dump current DataBuffer to string. Help debug.
     * 
     * @return string
     */
    String dump();

    int capacity();

    int position();

    DataBuffer position(int position);

    int limit();

    DataBuffer limit(int limit);

    DataBuffer mark();

    DataBuffer reset();

    DataBuffer clear();

    DataBuffer flip();

    DataBuffer rewind();

    int remaining();

    boolean hasRemaining();

    boolean isReadonly();

    boolean isBigEndian();

    DataBuffer setBigEndian(boolean b);

    boolean isDirect();

    DataBuffer compact();

    DataBuffer slice();

    DataBuffer duplicate();

    DataBuffer asReadOnlyBuffer();

    byte get();

    byte get(int index);

    DataBuffer get(byte[] dst);

    DataBuffer get(int index, byte[] dst);

    DataBuffer get(byte[] dst, int offset, int length);

    DataBuffer get(int index, byte[] dst, int offset, int length);

    DataBuffer get(ByteBuffer dst);

    DataBuffer get(ByteBuffer dst, int length);

    DataBuffer get(int index, ByteBuffer dst);

    DataBuffer get(int index, ByteBuffer dst, int length);

    DataBuffer get(DataBuffer dst);

    DataBuffer get(DataBuffer dst, int length);

    DataBuffer get(int index, DataBuffer dst);

    DataBuffer get(int index, DataBuffer dst, int length);

    DataBuffer put(byte b);

    DataBuffer put(int index, byte b);

    DataBuffer put(ByteBuffer src);

    DataBuffer put(ByteBuffer src, int length);

    DataBuffer put(int index, ByteBuffer src);

    DataBuffer put(int index, ByteBuffer src, int length);

    DataBuffer put(DataBuffer src);

    DataBuffer put(DataBuffer src, int length);

    DataBuffer put(int index, DataBuffer src);

    DataBuffer put(int index, DataBuffer src, int length);

    DataBuffer put(byte[] src);

    DataBuffer put(byte[] src, int offset, int length);

    DataBuffer put(int index, byte[] src);

    DataBuffer put(int index, byte[] src, int offset, int length);

    char getChar();

    DataBuffer putChar(char c);

    char getChar(int index);

    DataBuffer putChar(int index, char c);

    short getShort();

    DataBuffer putShort(short s);

    short getShort(int index);

    DataBuffer putShort(int index, short s);

    int getInt();

    DataBuffer putInt(int i);

    int getInt(int index);

    DataBuffer putInt(int index, int i);

    long getLong();

    DataBuffer putLong(long l);

    long getLong(int index);

    DataBuffer putLong(int index, long l);
    
    long getShortLong();

    DataBuffer putShortLong(long l);

    long getShortLong(int index);

    DataBuffer putShortLong(int index, long l);

    float getFloat();

    DataBuffer putFloat(float f);

    float getFloat(int index);

    DataBuffer putFloat(int index, float f);

    double getDouble();

    DataBuffer putDouble(double d);

    double getDouble(int index);

    DataBuffer putDouble(int index, double d);

    short getUnsignedByte();

    short getUnsignedByte(int index);

    DataBuffer putUnsignedByte(short s);

    DataBuffer putUnsignedByte(int index, short s);

    int getUnsignedShort();

    int getUnsignedShort(int index);

    DataBuffer putUnsignedShort(int i);

    DataBuffer putUnsignedShort(int index, int i);

    long getUnsignedInt();

    long getUnsignedInt(int index);

    DataBuffer putUnsignedInt(long l);

    DataBuffer putUnsignedInt(int index, long l);
    
    String getString();
    
    String getString(Charset charset);
    
    String getString(int length);

    String getString(Charset charset, int length);
    
    DataBuffer putString(String s);

    DataBuffer putString(String s, Charset charset);

    String getString(int index, Charset charset, int length);

    DataBuffer putString(int index, String s, Charset charset);

    int indexOf(byte[] b);
}
